/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package weather;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author administrator
 */
@WebService()
public class WeatherProvider {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getWeather")
    public String getWeather(@WebParam(name = "city") String city) {
        String weather = "";
        
        try {
            // Building the URL
            String url = "http://www.google.com/ig/api?weather=" + city + "&hl=fr&ie=utf-8&oe=utf-8";
            URL xmlUrl = new URL(url);

            System.out.println("URL : " + url);

            // Buidling the file to parse
            URLConnection urlCo = xmlUrl.openConnection();
            InputStream in = urlCo.getInputStream();

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            Document doc = db.parse(in);
            
            // Get current weather conditions
	        NodeList currentConditions = doc.getElementsByTagName("current_conditions");
	        Element current_cond = (Element) currentConditions.item(0);
	        Element weather_cond = (Element) current_cond.getFirstChild();
	        weather = weather_cond.getAttribute("data");
            
            return weather;
        } catch (MalformedURLException ex) {
            Logger.getLogger(WeatherProvider.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
             Logger.getLogger(WeatherProvider.class.getName()).log(Level.SEVERE, null, ex);
        }

        finally {
            return weather;
        }

    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getTemperature")
    public Float getTemperature(@WebParam(name = "city") String city) {
        Float temperature = 0.0f;
		
		try {
			String url = "http://www.google.com/ig/api?weather=" + city + "&hl=fr&ie=utf-8&oe=utf-8";
	        URL xmlUrl = new URL(url);

	        System.out.println("URL : " + url);

	        // Buidling the file to parse
	        URLConnection urlCo = xmlUrl.openConnection();
	        InputStream in = urlCo.getInputStream();

	        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        DocumentBuilder db = dbf.newDocumentBuilder();

	        Document doc = db.parse(in);
        
	        // Get current weather conditions
	        NodeList currentConditions = doc.getElementsByTagName("current_conditions");
	        Element current_cond = (Element) currentConditions.item(0);
			Element temp_c = current_cond.getElementsByTagName("temp_c").item(0)
	        temperature = Float.parseFloat(temp_c.getAttribute("data"));
        
	        return temperature;
	    } catch (MalformedURLException ex) {
	        Logger.getLogger(WeatherProvider.class.getName()).log(Level.SEVERE, null, ex);
	    } catch (Exception ex) {
	         Logger.getLogger(WeatherProvider.class.getName()).log(Level.SEVERE, null, ex);
	    }

	    finally {
	        return temperature;
	    }
	}
}
